home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter13 / SingleBuff.java < prev   
Text File  |  1995-12-31  |  3KB  |  91 lines

  1. /* Single buffering example */
  2. import java.awt.*;
  3.  
  4. public class SingleBuff extends java.applet.Applet implements Runnable {
  5.  
  6.    /* Variables to hold the dimensions of the applet */
  7.    protected int XRES,YRES;
  8.  
  9.    /* Width, height, and grid spacing */
  10.    protected int w=100,h=100,gw=10;
  11.  
  12.    /* X, Y, and previous Y positions */
  13.    protected int x,y=1,oldy=1;
  14.  
  15.    Thread MyThread;    // main thread for repaints
  16.    /* lx..lw are for concentric circles */
  17.    public int lx,ly,lh,lw;
  18.  
  19.    public Rectangle AppBorder;
  20.  
  21.    /* The applet init method */
  22.    public void init() {
  23.         AppBorder = bounds();     // get current applet 
  24.         XRES = AppBorder.width;
  25.         YRES = AppBorder.height;
  26.         x = XRES/2-w/2;         // center the image
  27.         }
  28.  
  29.    /* The paint method */
  30.    public void paint(Graphics g) {
  31.  
  32.       g.setColor(Color.lightGray); // background color
  33.       g.fillRect(x,oldy,w+1,h+1);  // cover up last box drawn
  34.       oldy=y;               // remember y for the next coverup
  35.  
  36.       /* draw background pattern */
  37.       for(int gc=0;gc<XRES;gc+=gw) { 
  38.         g.setColor(Color.gray);        // color of gridlines
  39.         g.drawLine(gc,0,gc,YRES);      // draw vertical lines
  40.         g.drawLine(0,gc,XRES,gc);      // draw horizontal lines
  41.         g.setColor(Color.yellow);      // set color to yellow
  42.         g.drawLine(gc,0,0,gc);         // draw diags
  43.         g.setColor(Color.blue);        // set color to blue
  44.         g.drawLine(XRES-gc,0,XRES,gc); // draw diags
  45.         }
  46.  
  47.       g.setColor(Color.lightGray); // background color
  48.       g.fillRect(0,0,100,20);      // clear box for title
  49.       g.setColor(Color.red);       // set color to red
  50.       g.drawString("SingleBuffer",0,10);
  51.       g.fillRect(x,y,w,h);        // draw red square
  52.       g.setColor(Color.blue); 
  53.  
  54.       int l=0;
  55.       for (int lh=h;lh>0;l+=4,lh-=8)   
  56.     g.drawOval(x+l,y+l,lh,lh);
  57.  
  58.    }
  59.  
  60.    public void start() {
  61.       if(MyThread==null) { // start a new thread if not already running
  62.       MyThread = new Thread(this);
  63.       MyThread.start();
  64.       }
  65.    }
  66.  
  67.    public void update(Graphics g) { 
  68.    // override update to avoid clearing the screen
  69.     paint(g); 
  70.    } 
  71.  
  72.    public void stop() {
  73.       if (MyThread != null) MyThread.stop();
  74.       MyThread = null; //kill thread when applet is stopped
  75.    }
  76.  
  77.    public void run() { // the run loop. 
  78.     int yd = 1;
  79.  
  80.       while(true) {
  81.     y += yd;
  82.         if (y<=1 || y >= YRES) yd = -yd;
  83.  
  84.       try {Thread.sleep(5);} catch(InterruptedException e){}
  85.       this.repaint(); //redraw the screen
  86.       }
  87.    }
  88. }
  89.  
  90.  
  91.